package com.pocketsweet.ui.fragments;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.lidroid.xutils.ViewUtils;
import com.pocketsweet.R;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.model.MLVoice;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.EditBelong;
import com.pocketsweet.ui.EditCall;
import com.pocketsweet.ui.EditCalled;
import com.pocketsweet.ui.EditLikeSytle;
import com.pocketsweet.ui.EditVoice;
import com.pocketsweet.ui.EditWilling;
import com.pocketsweet.ui.Main;
import com.pocketsweet.ui.uilib.FlowLayout;
import com.pocketsweet.ui.uilib.LoadingDailog;
import com.pocketsweet.ui.uilib.RandomColorWillingTag;
import com.pocketsweet.ui.uilib.adapter.VoiceAdapter;
import com.pocketsweet.utils.StringUtils;

public class LoveSkillEditFragment extends Fragment implements OnClickListener {

	private TextView tvBelong;
	private TextView tvLikeType;
	private TextView tvCallMe;
	private TextView tvCallYou;
	private LinearLayout linBelong;
	private LinearLayout linLikeType;
	private LinearLayout linCallMe;
	private LinearLayout linCallYou;
	private LinearLayout linWantU;
	private LinearLayout linVoice;
	private FlowLayout willingTag;
	private ListView voiceListView;

	private LoadingDailog loading;
	private VoiceAdapter voiceAdapter;
	private MLUser mlUser = new MLUser();
	private ArrayList<String> calledList = new ArrayList<String>();// 将值传给编辑页面，编辑页面不用再请求网络
	private ArrayList<String> callList = new ArrayList<String>();// 将值传给编辑页面，编辑页面不用再请求网络
	private ArrayList<String> willingList; // 将值传给编辑页面，编辑页面不用再请求网络

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_love_skill_edit,
				null);
		ViewUtils.inject(this, rootView);
		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initView();
	}

	@Override
	public void onResume() {
		super.onResume();
		initVoice();
	}

	@Override
	public void onStart() {
		super.onStart();
	}

	@Override
	public void onPause() {
		super.onPause();
		if (Main.MediaTimer != null && Main.mMediaPlayer.isPlaying()) {
			Main.mMediaPlayer.pause();
			Main.mMediaPlayer.stop();
			Main.mMediaPlayer.reset();
			Main.MediaTimer.cancel();
		}
	}

	private void initView() {
		tvBelong = (TextView) getActivity().findViewById(R.id.tvBelong);
		tvLikeType = (TextView) getActivity().findViewById(R.id.tvLikeType);
		tvCallMe = (TextView) getActivity().findViewById(R.id.tvCallMe);
		tvCallYou = (TextView) getActivity().findViewById(R.id.tvCallYou);
		linBelong = (LinearLayout) getActivity().findViewById(R.id.linBelong);
		linVoice = (LinearLayout) getActivity().findViewById(R.id.linVoice);
		linLikeType = (LinearLayout) getActivity().findViewById(
				R.id.linLikeType);
		linCallMe = (LinearLayout) getActivity().findViewById(R.id.linCallMe);
		linCallYou = (LinearLayout) getActivity().findViewById(R.id.linCallYou);
		linWantU = (LinearLayout) getActivity().findViewById(R.id.linWantU);
		willingTag = (FlowLayout) getActivity().findViewById(R.id.willingTag);
		voiceListView = (ListView) getActivity().findViewById(
				R.id.voiceListView);
		voiceAdapter = new VoiceAdapter(getActivity(), Main.voiceList, false);
		linBelong.setOnClickListener(this);
		linLikeType.setOnClickListener(this);
		linCallMe.setOnClickListener(this);
		linCallYou.setOnClickListener(this);
		linVoice.setOnClickListener(this);
		linWantU.setOnClickListener(this);
	}

	private void initVoice() {
		// if (Main.voiceList == null) {
		mlUser = UserService.getCurrentUser();
		final AVQuery<MLVoice> query = new AVQuery<MLVoice>("MLVoice");
		query.whereEqualTo("belong_to", mlUser);
		query.findInBackground(new FindCallback<MLVoice>() {
			@Override
			public void done(List<MLVoice> list, AVException arg1) {
				if (arg1 == null) {
					Main.voiceList.clear();
					if (list.size() > 0) {
						for (MLVoice temp : list) {
							Main.voiceList.add(temp);
						}
						voiceAdapter.notifyDataSetChanged();
					}
					getUserInfo();
				} else {
					getUserInfo();
					// Log.e("＝＝错误", "查询错误: " + arg1.getMessage());

				}
			}
		});
		// }
	}

	/*
	 * 个人资料
	 */
	private void getUserInfo() {
		mlUser = UserService.getCurrentUser();
		voiceAdapter.notifyDataSetChanged();
		voiceListView.setAdapter(voiceAdapter);
		if (!StringUtils.isEmpty(mlUser.getBelong())) {
			tvBelong.setText(mlUser.getBelong());
		}
		if (!StringUtils.isEmpty(mlUser.getLike())) {
			tvLikeType.setText(mlUser.getLike());
		}
		if (mlUser.getCalledTags() != null && mlUser.getCalledTags().size() > 0) {
			StringBuffer called = new StringBuffer();
			for (String temp : mlUser.getCalledTags()) {
				called.append(temp + " ");
			}
			tvCallMe.setText(called);
		} else {
			tvCallMe.setText("");
		}
		if (mlUser.getCallTags() != null && mlUser.getCallTags().size() > 0) {

			StringBuffer call = new StringBuffer();
			for (String temp : mlUser.getCallTags()) {
				call.append(temp + " ");
			}
			tvCallYou.setText(call);
		} else {
			tvCallYou.setText("");
		}
		if (mlUser.getWillingTags() != null
				&& mlUser.getWillingTags().size() > 0) {
			willingList = new ArrayList<String>();
			for (String temp : mlUser.getWillingTags()) {
				willingList.add(temp);
			}
			initWillingTag(willingList);
		} else {
			willingTag.removeAllViews();
		}
	}

	private void initWillingTag(ArrayList<String> willingList) {
		willingTag.removeAllViews();
		for (int i = 0; i < willingList.size(); i++) {
			final RandomColorWillingTag itemView = new RandomColorWillingTag(
					getActivity());
			itemView.setText(willingList.get(i));
			willingTag.addView(itemView);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.linBelong:
			Intent intent = new Intent(getActivity(), EditBelong.class);
			intent.putExtra("belong", mlUser.getBelong());
			startActivity(intent);
			break;
		case R.id.linLikeType:
			Intent intent1 = new Intent(getActivity(), EditLikeSytle.class);
			intent1.putExtra("like", mlUser.getLike());
			startActivity(intent1);
			break;
		case R.id.linCallMe:
			Intent intent2 = new Intent(getActivity(), EditCalled.class);
			startActivity(intent2);
			break;
		case R.id.linCallYou:
			Intent intent3 = new Intent(getActivity(), EditCall.class);
			startActivity(intent3);
			break;
		case R.id.linVoice:
			Intent intent4 = new Intent(getActivity(), EditVoice.class);
			startActivity(intent4);
			break;
		case R.id.linWantU:
			Intent intent5 = new Intent(getActivity(), EditWilling.class);
			startActivity(intent5);
			break;

		}
	}
}